<?php

if (isset($_GET['idActivity']) && isset($_GET['actualWeek']) && isset($_GET['session']))
{
	$session = $_GET['session'];
	$idActivity = $_GET['idActivity'];
	$actualWeek = $_GET['actualWeek'];
	$actualWeek = $actualWeek + 32;
	if ($actualWeek > 52) $actualWeek -= 52;
	
	function cmpTabEvent($a, $b)
	{
		$retour = -1;
		if( $a->attributes->getNamedItem("week")->value > $b->attributes->getNamedItem("week")->value )
			$retour = 1;
		else if ( ($a->attributes->getNamedItem("week")->value == $b->attributes->getNamedItem("week")->value))
		{
			if ($a->attributes->getNamedItem("day")->value > $b->attributes->getNamedItem("day")->value)
				$retour = 1;
			else if ($a->attributes->getNamedItem("day")->value == $b->attributes->getNamedItem("day")->value && $a->attributes->getNamedItem("slot")->value > $b->attributes->getNamedItem("slot")->value)
				$retour = 1;
		}
		return $retour;
	}

	$url = 'http://ade52-savoie.grenet.fr/ade/webapi?function=connect&login=ETUDIANT&password=etsa';
	$agent = '';
	$id = '';
		
	$url = "http://ade52-savoie.grenet.fr/ade/webapi?sessionId=$session&function=getActivities&id=$idActivity&detail=17"; //getEvents&resources=1165&detail=4&weeks=12&days=0
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, $agent);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 8);   
	curl_setopt($ch, CURLOPT_COOKIE, $id);
 
	$ret = curl_exec($ch);
	if (curl_errno($ch))
	{
		print curl_error($ch);
		return false;
	}
	curl_close($ch);
	
	echo $ret;
	
	
	$page = new DOMDocument();
	$page->loadXML($ret);
	
	$activity = $page->getElementsByTagName("activity")->item(0);
	$nom = $activity->attributes->getNamedItem("codeX")->value ;
	$type =  $activity->attributes->getNamedItem("type")->value ;
	$nbreCours =  $activity->attributes->getNamedItem("nbEvents")->value ;
	$dureeTot =  $activity->attributes->getNamedItem("duration")->value ;
	$dureeTot = $dureeTot/60;
	$lien =  $activity->attributes->getNamedItem("url")->value ;
	
	
	
	echo "<h3 class='infosCours'>";
	if ($lien) echo "<a class='infosCours' href='$lien' target='blank'>";
	echo "$nom";
	if ($lien) echo "</a>";
	echo "</h3>";
	echo "<p class='infosCours'>Type de cours : $type";
	//if ($lien) echo "<br/>Lien</a>";
	echo "<br/>Dur&egrave;e du cours : $dureeTot h";
	//echo "<br/>Nombre de cours : $nbreCours";
	echo "</p>";
	
	$events = $page->getElementsByTagName("event");
	$tabEvent = array();
	for ($j = 0; $j < $events->length; $j++)
	{
		$tabEvent[$j] = $events->item($j);
	}
	usort( $tabEvent , "cmpTabEvent" );
	
	echo "<table class='infosCours tabCours' ><tbody class='infosCours'>";
	echo "<tr class='infosCours tabImpair'><th class='infosCours'>Date</th><th class='infosCours'>Professeur</th><th class='infosCours'>Salle</th>";
	for ($j = 0; $j < count($tabEvent); $j++)
	{
		//$detailCours = $tabEvent[$j]->attributes->getNamedItem("name")->value ; // 4 - Nom du cours
		$heureDebut = $tabEvent[$j]->attributes->getNamedItem('slot')->value ; // 6 - Heure du cours
		$duree = $tabEvent[$j]->attributes->getNamedItem('duration')->value ; // 10 - Dur�e du cours
		$semaineTemp = $tabEvent[$j]->attributes->getNamedItem('week')->value ;
		$jour = $tabEvent[$j]->attributes->getNamedItem('day')->value ;
		$jour ++;
		$semaine = $semaineTemp + 32;
		if ($semaine > 52) $semaine -= 52;
		//echo $semaine;
		//echo $actualWeek;
		
		$annee = ($semaine<35)? date('Y')+1 : date('Y');
		//echo "$semaine - $jour - $annee"; 
		$date = trouver_date($semaine,  $annee, $jour);
		
		$heureDebutTot = $heureDebut * 1/4 + 7.5;
		$heureDebutH = floor($heureDebutTot);
		$heureDebutM = ($heureDebutTot - $heureDebutH) * 60;
		
		$heureFinTot = $heureDebutTot + $duree * 1/4;
		$heureFinH = floor($heureFinTot);
		$heureFinM = ($heureFinTot - $heureFinH) * 60;
		
		$ressourcesTemp = $tabEvent[$j]->childNodes->item(1);
		$ressources = $ressourcesTemp->childNodes;
		
		
		foreach ($ressources as $ressource)
		{
			if ($ressource->hasAttributes())
			{
				if ($ressource->attributes->item(4)->value == 'instructor')
					$prof = $ressource->attributes->item(5)->value;
				if ($ressource->attributes->item(4)->value == 'classroom')
					$salle = $ressource->attributes->item(5)->value;
			}
			
		}
		$pairImpair = "tabImpair";
		if ($j %2 == 0) $pairImpair = "tabPair";
		$b = ""; $b2 = "";
		if ($actualWeek == $semaine) { $b = "<b class='infosCours'>"; $b2 = "</b>"; }
		echo "<tr class='infosCours $pairImpair'><td class='infosCours'>$b $date $b2</td><td class='infosCours'>$b $prof $b2</td><td class='infosCours'>$b $salle $b2</td></tr>";
	}
	echo "</tbody></table>";
		
	//}
	//echo $ret;
	//echo $session;
}

function trouver_date($sem, $annee, $j)
{
	$jour_dans_le_mois = array();
	$mois = 1;
	$jour = 0;
	
	for($a=1;$a<=365+date('L');$a++)
	{ 
	for($i=1;$i<=12;$i++)
	{ 
		$jour += date('t', mktime(0, 0, 0, $i, 1, date('Y')));
		$jour_dans_le_mois[$i] = $jour;           
	} 
			 
	$today = mktime(0, 0, 0, $mois, $a, date('Y'));        
	$semaine = date('W', $today);
	$day = date('N', $today);
	$date = date('d/m/Y', $today);
	if(in_array($a, $jour_dans_le_mois)) $mois++;
			 
	if($semaine == $sem && $day == $j)
		return $date;
	}
}
?>










